$(document).ready(function() {
    google.load("language", "1");
    initLocalStorage()
    chrome.contextMenus.create({
        "title": "Translate",
        "contexts": ["selection"],
        "onclick": function(data) {
            var text = data.selectionText
            translate(text, defaultCallback)
        }
    })
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
        if (!sender.tab) return
        if (request.type == "gethotkey") {
//            alert("bg received request")
            sendResponse({
                value: localStorage["hotkey_value"],
                shift: localStorage["hotkey_shift"],
                ctrl: localStorage["hotkey_ctrl"],
                alt: localStorage["hotkey_alt"],
                useCtrl: localStorage["use_ctrl"]
            })
        } else if (request.type == "translate") {
            translate(request.text, defaultCallback)
        }
    })
})

function replaceTextInDOM(newText, oldText) {
    chrome.tabs.executeScript(null, {code: "document.body.innerHTML = document.body.innerHTML.replace('"+oldText+"', ' "+newText+" ');"})
}

function translate(text, callback) {
    google.language.detect(text, function(detectResult) {
        var userLanguage = window.navigator.language
        google.language.translate(text, detectResult.language, userLanguage, function(translateResult) {
            callback(translateResult, text, detectResult.language, userLanguage)
        })
    })
}

function notify(text) {
    var notification = webkitNotifications.createNotification("icon.png", "Translation", text)
    notification.show()
    setTimeout(function() {notification.cancel()}, 10000)
}

function sendResultToContentScript(text, ignored, srcLanguage, destLanguage) {
    var tab = chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {
            type: "translation",
            text: text,
            title: srcLanguage + " > " + destLanguage
        })
    })
}

function initLocalStorage() {
    if (localStorage["hotkey_value"] == undefined) localStorage["hotkey_value"] = 81
    if (localStorage["hotkey_shift"] == undefined) localStorage["hotkey_shift"] = false
    if (localStorage["hotkey_ctrl"] == undefined) localStorage["hotkey_ctrl"] = true
    if (localStorage["hotkey_alt"] == undefined) localStorage["hotkey_alt"] = false
    if (localStorage["output"] == undefined) localStorage["output"] = "alert"
    if (localStorage["use_ctrl"] == undefined) localStorage["use_ctrl"] = false
}

function defaultCallback(result, text, srcLanguage, destLanguage) {
    var output = localStorage["output"]
    switch(output) {
        case "alert": alert(result.translation); break
        case "notification": notify(result.translation); break
        case "replace": replaceTextInDOM(result.translation, text); break
        case "div": sendResultToContentScript(result.translation, text, srcLanguage, destLanguage); break
        default: alert("unknown option")
    }
//                notify(result.translation)
//                show(result.translation)
//                replaceTextInDOM(result.translation, text)
}